﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {      
        public void GetAllChucVu(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblChucVus.Where(x => x.TenCV.Contains(strTimkiem)).OrderBy(x => x.TenCV);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaCV;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenCV;
                    dataGridViewX.Rows[i].Cells[3].Value = item.GhiChu;
                    i++;
                }
            }
        }

        public tblChucVu GetChucVuById(string machucvu)
        {
            return _dal.tblChucVus.Where(x => x.MaCV == machucvu).FirstOrDefault();
        }

        public void InsertChucVu(tblChucVu chucvu)
        {
            _dal.tblChucVus.AddObject(chucvu);
            _dal.SaveChanges();
        }

        public void UpdateChucVu(string maChucVuCu, tblChucVu chucvu)
        {
            var CVUpdate = _dal.tblChucVus.Where(x => x.MaCV == maChucVuCu).FirstOrDefault();
            CVUpdate.MaCV = chucvu.MaCV;
            CVUpdate.TenCV = chucvu.TenCV;
            CVUpdate.GhiChu = chucvu.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteCV(IEnumerable<string> machucvus)
        {
            var listDelete = _dal.tblChucVus.Where(x => machucvus.Contains(x.MaCV));
            foreach (var tblchucvu in listDelete)
            {
                _dal.tblChucVus.DeleteObject(tblchucvu);
            }
            _dal.SaveChanges();
        }

        public tblChucVu BuildCV(string maCV, string tenCV, string ghichu)
        {
            return new tblChucVu { MaCV = maCV, TenCV = tenCV,GhiChu = ghichu };
        }
    }
}
